package bibliotheque;

import bibliotheque.events.ErrorOccuredEvent;
import bibliotheque.events.ErrorOccuredListener;
import bibliotheque.events.StateChangedEvent;
import bibliotheque.events.StateChangedListener;
import bibliotheque.nameserver.NameServer;
import bibliotheque.server.Server;
import java.io.File;
import java.util.Date;

public class Main extends Thread implements ErrorOccuredListener, StateChangedListener{
    
    public Main() {
        
    }
    
    public void run() {
        try {
            RMIRegistry.Start(2222);
            /*
            new NameServerGui().setVisible(true);
            new ServerGui().setVisible(true);
            new ServerGui().setVisible(true);
            new ServerGui().setVisible(true);
             */
            
            NameServer annuaire = new NameServer();
            Server server1 = new Server("192.168.0.1");
            Server server2 = new Server("192.168.0.2");
            Server server3 = new Server("192.168.0.3");
            Server server4 = new Server("192.168.0.4");
            Server server5 = new Server("192.168.0.5");
            Server server6 = new Server("192.168.0.6");
            Server server7 = new Server("192.168.0.7");
            Server server8 = new Server("192.168.0.8");
            Server server9 = new Server("192.168.0.9");
            Server server10 = new Server("192.168.0.10");
            annuaire.AddErrorOccuredListener(this);
            annuaire.AddStateChangedListener(this);
            server1.AddErrorOccuredListener(this);
            server1.AddStateChangedListener(this);
            server2.AddErrorOccuredListener(this);
            server2.AddStateChangedListener(this);
            server3.AddErrorOccuredListener(this);
            server3.AddStateChangedListener(this);
            server4.AddErrorOccuredListener(this);
            server4.AddStateChangedListener(this);
            server5.AddErrorOccuredListener(this);
            server5.AddStateChangedListener(this);
            server6.AddErrorOccuredListener(this);
            server6.AddStateChangedListener(this);
            server7.AddErrorOccuredListener(this);
            server7.AddStateChangedListener(this);
            server8.AddErrorOccuredListener(this);
            server8.AddStateChangedListener(this);
            server9.AddErrorOccuredListener(this);
            server9.AddStateChangedListener(this);
            server10.AddErrorOccuredListener(this);
            server10.AddStateChangedListener(this);
            annuaire.StartService(new File("src/bibliotheque/config/configannuaire.xnc"), "bibliotheque", "127.0.0.1", 2222);
            server1.StartService(new File("src/bibliotheque/config/configserveur1.xsc"), "bibliotheque", "127.0.0.1", 2222);
            server2.StartService(new File("src/bibliotheque/config/configserveur2.xsc"), "bibliotheque", "127.0.0.1", 2222);
            server3.StartService(new File("src/bibliotheque/config/configserveur3.xsc"), "bibliotheque", "127.0.0.1", 2222);
            server4.StartService(new File("src/bibliotheque/config/configserveur4.xsc"), "bibliotheque", "127.0.0.1", 2222);
            server5.StartService(new File("src/bibliotheque/config/configserveur2.xsc"), "bibliotheque", "127.0.0.1", 2222);
            server6.StartService(new File("src/bibliotheque/config/configserveur3.xsc"), "bibliotheque", "127.0.0.1", 2222);
            server7.StartService(new File("src/bibliotheque/config/configserveur4.xsc"), "bibliotheque", "127.0.0.1", 2222);
            server8.StartService(new File("src/bibliotheque/config/configserveur1.xsc"), "bibliotheque", "127.0.0.1", 2222);
            server9.StartService(new File("src/bibliotheque/config/configserveur2.xsc"), "bibliotheque", "127.0.0.1", 2222);
            server10.StartService(new File("src/bibliotheque/config/configserveur3.xsc"), "bibliotheque", "127.0.0.1", 2222);
            server1.StopService();
            this.sleep(10000);
            server2.StopService();
            this.sleep(10000);
            server3.StopService();
            this.sleep(10000);
            server4.StopService();
            this.sleep(10000);
            server5.StopService();
            this.sleep(10000);
            server6.StopService();
            this.sleep(10000);
            server1.StartService(new File("src/bibliotheque/config/configserveur1.xsc"), "bibliotheque", "127.0.0.1", 2222);
            this.sleep(10000);
            server7.StopService();
            this.sleep(10000);
            server8.StopService();
            this.sleep(10000);
            server9.StopService();
            this.sleep(10000);
            server10.StopService();
            this.sleep(10000);
            server1.StopService();
        } catch (Exception e) {
            e.printStackTrace();
            System.exit(1);
        }
    }
    
    public void ErrorOccured(ErrorOccuredEvent event) {
        Server server;
        if(event.getSource() instanceof NameServer) {
            System.out.println("nameServer : " + event.GetError() + "  " + (new Date()).toString());
        } else if(event.getSource() instanceof Server) {
            server = (Server)event.getSource();
            System.out.println(server.GetIp() + " : " + event.GetError() + "  " + (new Date()).toString());
        }
    }
    
    public void StateChanged(StateChangedEvent event) {
        Server server;
        if(event.getSource() instanceof NameServer) {
            System.out.println("nameServer : " + event.GetState() + "  " + (new Date()).toString());
        } else if(event.getSource() instanceof Server) {
            server = (Server)event.getSource();
            System.out.println(server.GetIp() + " : " + event.GetState() + "  " + (new Date()).toString());
        }
    }
    
    public static void main(String[] args) {
        new Main().start();
    }
}
